package practise.mulitThreads.ConcurrentInPractice.secondTime.CustomerizedCounter;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Richard on 7/28/2014.
 */
public class SimulatedCAS {
    private Lock lock = new ReentrantLock();
    int value =0;
    private int compareAndSwap(int expect, int update){
        try{
            lock.lock();
            if(expect==value){
                value=update;
            }
            return value;
        }
        finally {
            lock.unlock();
        }
    }

    public boolean compareAndSet(int expect, int update){
        return (compareAndSwap(expect,update)==expect);
    }

    public Integer get(){
        return value;
    }
}
